package org.datavis.data;
import java.util.ArrayList;

public class Data implements Cloneable {
	
	protected ArrayList<ArrayList<Double>> values;
	protected ArrayList<String> fields;
	protected ArrayList<String> descriptions;
	protected double valueRange = 0.;
	protected double maxValue = 0.;
	protected double minValue = 0.;
	protected int valuesPerEntry = 0;
	
	public Data(){
		values = new ArrayList<ArrayList<Double>>();
		fields = new ArrayList<String>();
	}
	
	public Data clone()
	{
		Data data = new Data();
		data.values = new ArrayList<ArrayList<Double>>();
		for(ArrayList<Double> item: values)
			data.values.add((ArrayList<Double>)item.clone());
		data.fields = (ArrayList<String>)fields.clone();
		data.descriptions = (ArrayList<String>)descriptions.clone();
		data.calculateDataValues();
		return data;
	}
	
	public ArrayList<String> getDescriptions() {
		return descriptions;
	}

	public void setDescriptions(ArrayList<String> descriptions) {
		this.descriptions = descriptions;
	}
	
	public double getValueRange() {
		return valueRange;
	}
	
	public double getMaxValue() {
		return maxValue;
	}
	
	public double getMinValue() {
		return minValue;
	}
	
	public int getValuesPerEntry() {
		return valuesPerEntry;
	}

	public ArrayList<ArrayList<Double>> getValues() {
		return values;
	}
	
	public ArrayList<String> getFields() {
		return fields;
	}
	
//	/**
//	 * sorts data by first available value
//	 */
//	public void sort(){
//		
//	}
//	
//	/**
//	 * sorts data by given sort field
//	 */
//	public void sortBy(String sortField)
//	{
//		
//	}
	
	public void setDesc(String descriptions[] )
	{
		ArrayList<String> tmp = new ArrayList<String>();		
		for (String item: descriptions)
			tmp.add(item);
		this.descriptions = tmp;
	}	

	public void addData(String field, ArrayList<Double> values )
	{
		this.fields.add(field);
		this.values.add(values);
		calculateDataValues();
	}
	
	public void addData(String field, double values[]) {
		ArrayList<Double> tmp = new ArrayList<Double>();		
		for (Double item: values)
			tmp.add(item);
		this.values.add(new ArrayList<Double>());
		this.values.get(this.values.size()-1).addAll(tmp);
		this.fields.add(field);
		calculateDataValues();
	}
	
	public void addData(String field, int values[]) {
		
		ArrayList<Double> tmp = new ArrayList<Double>();		
		for (Integer item: values)
			tmp.add((double)(item));
		this.fields.add(field);
		this.values.add(tmp);
		calculateDataValues();
	}
	
	protected void calculateDataValues()
	{
		valuesPerEntry = 0;
		//sort Values to get max and min value;
		ArrayList<Double> tmp = new ArrayList<Double>();
		
		for (ArrayList<Double> item: values)
		{
			if(item.size() > valuesPerEntry)
				valuesPerEntry = item.size();
			tmp.addAll(item);
		}
		java.util.Collections.sort(tmp);
				
		minValue = tmp.get(0); 
		maxValue = tmp.get(tmp.size()-1);
		
		tmp = null;
		
		//calculate value range and origin
		if(maxValue > 0. && minValue < 0.)
		{
			valueRange = maxValue - minValue;			
		}
		else if (maxValue > 0.)
		{
			valueRange = maxValue;		
		}
		else if (maxValue < 0)
		{
			valueRange = -minValue;
		}  
	}
	
	public void removeData(int index)
	{
		for(ArrayList<Double> item: values)
		{
			//item.remove(index);
			item.remove(index);
		}
		descriptions.remove(index);
		calculateDataValues();		
	}

}
